import { reqCreateEquipment, reqDeleteEquipmentByIds, reqGetEquipmentByCondition, reqUpdateEquipmentById, reqUpdateEquipmentStateByNowTime } from "@/api";
import {Notification,Message} from 'element-ui';
export default({
    namespaced:true,
    actions:{
        async getEquipmentByCondition(context,value){
            let result=await reqGetEquipmentByCondition(value);
            if(result.code==='200'){
                context.commit('getEquipmentByCondition',result.other);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async createEquipment(context,value){
            let result=await reqCreateEquipment(value);
            if(result.code==='200'){
                context.commit('createEquipment');
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async updateEquipmentById(context,value){
            let result=await reqUpdateEquipmentById(value);
            if(result.code==='200'){
                context.commit('updateEquipmentById',value);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async deleteEquipmentByIds(context,value){
            let result=await reqDeleteEquipmentByIds(value);
            if(result.code==='200'){
                context.commit('deleteEquipmentByIds',value);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async updateEquipmentStateByNowTime(context,value){
            let result=await reqUpdateEquipmentStateByNowTime(value);
            if(result.code==='200'){
                Notification({
                    title: '通知',
                    message: '报废设备状态更新成功',
                    type: 'success'
                });
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        }
    },
    mutations:{
        getEquipmentByCondition(state,value){
            state.equipmentList=value.equipments;
            state.equipmentListRows=value.rows;
        },
        createEquipment(){
            location.reload();
        },
        updateEquipmentById(state,value){
            let list=state.equipmentList;
            list.forEach(element => {
                    if(element.id===value.id){
                        element.equname=value.equname;
                        element.roomid=value.roomid;
                        element.roomno=value.roomno;
                        element.createtime=value.createtime;
                    }
            });
        },
        deleteEquipmentByIds(state,value){
            let list=state.equipmentList;
            list.forEach((element,index) => {
                if(value.includes(element.id)){
                    list.splice(index,1)
                }
            })
        }
    },
    state:{
        equipmentListRows:0,
        equipmentList:[]
    },
    getters:{

    }
})